package com.app.view.manager 
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.system.ApplicationDomain;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import com.peachcastle.core.SoundData;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class SoundManagerMediator extends Mediator implements IMediator
	{
		public static const NAME:String 					= "SoundManagerMediator";
		public static const PLAY:String 					= NAME + "_Play";
		public static const STOP:String 					= NAME + "_Stop";
		public static const ADD_BACKGROUND:String 			= NAME + "_AddBackground";
		public static const PLAY_BACKGROUND:String 			= NAME + "_PlayBackground";
		public static const STOP_BACKGROUND:String 			= NAME + "_StopBackground";
		
		public static const SEAT_SIT:String 				= "SeatSitSound";
		public static const SEAT_STAND:String 				= "SeatStandSound";
		public static const SHAKER_DICE:String 				= "ShakerDiceSound";//摇骰子
		public static const SMALL_CHIP:String 				= "SmallChipSound";
		public static const MIDDLE_CHIP:String 				= "MiddleChipSound";
		public static const BIG_CHIP:String 				= "BigChipSound";
		
		
		public var _soundOn:int = 1;
		private var _sound:Sound = null;
		private var _channel:SoundChannel = null;
		
		public var _bgSoundOn:int = 1;
		private var _soundData:SoundData = null;
		private var _connection:Object =  null;
		
		public function SoundManagerMediator() 
		{
			super(NAME);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				SoundManagerMediator.PLAY,
				SoundManagerMediator.STOP,
				SoundManagerMediator.ADD_BACKGROUND,
				SoundManagerMediator.PLAY_BACKGROUND,
				SoundManagerMediator.STOP_BACKGROUND
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var param:* = notification.getBody();

			switch ( name )
			{
				case SoundManagerMediator.PLAY:
					if (param.length <= 1) param[1] = 1;
					play(param[0], param[1]);
					
					break;
					
				case SoundManagerMediator.STOP:
					stop();
					
					break;
				
				case SoundManagerMediator.ADD_BACKGROUND:
					addExternalSound(param[0], param[1]);
					playExternalSound(param[1]);
					
					break;
					
				case SoundManagerMediator.PLAY_BACKGROUND:
					playExternalSound(param[0]);
					
					break;
					
				case SoundManagerMediator.STOP_BACKGROUND:
					stopExternalSound(param[0]);
					
					break;
			}
		}
		
		/**
		 * 播放音效
		 * @param	loops		循环数量
		 * @param	soundName	音乐名称
		 */
		private function play(soundName:String, loops:int = 0):void
		{
			if (_soundOn == 0) return;
			
			_sound = getGameSound(soundName);
			if (_sound)
			{
				_channel = _sound.play(0, loops);
			}
		}
		
		/**
		 * 停止播放当前音效
		 */
		private function stop():void
		{
			if (_channel == null) return;

			_channel.stop();
		}
		
		/**
		 * 获取声音
		 * @param	className
		 * @return
		 */
		private function getGameSound(className:String):Sound
		{
			var GameSound:Class = ApplicationDomain.currentDomain.getDefinition(className) as Class;
			return (new GameSound()) as Sound;
		}
		
		private function addExternalSound(url:String = "", soundName:String = ""):void
		{
			if (_bgSoundOn == 0) return;
			if (url == "") return;
			
			var data:SoundData = new SoundData(url);
			
			if (!_connection) _connection = { };
			_connection[soundName] = data;
			
			data = null;
		}
		
		private function playExternalSound(soundName:String):void
		{
			if (_bgSoundOn == 0) return;
			if (!_connection || !_connection.hasOwnProperty(soundName)) return;
			
			if (SoundData(_connection[soundName]).iscomplete)
			{
				SoundData(_connection[soundName]).play();
			}else
			{
				SoundData(_connection[soundName]).start();
			}
		}
		
		private function stopExternalSound(soundName:String):void
		{
			if (_bgSoundOn == 0) return;
			if (!_connection || !_connection.hasOwnProperty(soundName)) return;
			
			SoundData(_connection[soundName]).stop();
		}
	}

}